package com.sankuai.inf.leaf.server.util;

import java.util.Random;

public class RandomHelper {
    
    private RandomHelper() {}
    
    private static final long[] FACTOR_TABLE = {
        0L,
        1L,
        10L,
        100L,
        1000L,
        10000L,
        100000L,
        1000000L,
        10000000L,
        100000000L,
        1000000000L,
        10000000000L,
        100000000000L,
        1000000000000L,
        10000000000000L,
        100000000000000L,
        1000000000000000L,
        10000000000000000L,
        100000000000000000L,
        1000000000000000000L
    };
    
    /**
     * 生成随机数字。
     * 
     * @param length
     *            区间为[1,19]超过范围将返回0
     * @return
     */
    public static long getRandomNumber(int length) {
        if (1 > length || 19 < length) {
            return 0;
        }
        return (long) ((Math.random() * 9 + 1) * FACTOR_TABLE[length]);
    }
    
    public static String getRandomString(int length) { // length表示生成字符串的长度
        String base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    
}
